// 1. 导入mongoose
const mongoose = require('mongoose');
// 2. 连接数据库
// url: 协议://ip:端口/数据库名
mongoose.connect("mongodb://localhost/0907")
// 3. 创建连接回调 connection.on("open",callback)
mongoose.connection.on('open',()=>{
    console.log("连接数据库成功");
    // 5. 创建文档结构 new mongoose.Schema()
    const userSchema = new mongoose.Schema({
        name:String,
        age:Number,
        score:Number
    })
    // 6. 创建模型对象 mongoose.model()
    // 参数1：集合名称
    // 参数2：文档结构
    const userModel = mongoose.model('users',userSchema)
    // 7. 批量插入 model.insertMany({},callback)
    userModel.insertMany([
        {
            name:"刘斌南",
            age:18,
            score:99
        },
        {
            name:"马鹏博",
            age:23,
            score:19
        },
        {
            name:"赵超",
            age:10,
            score:39
        }
    ],(err,data)=>{
        if(err) return console.log("插入失败");

        console.log(data);

    })
})

mongoose.connection.on('error',()=>{
    console.log("连接数据库失败");
})
    